import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/03-login'
import Find from '@/views/02-find'
import Index from '@/views/01-index'
import Zixun from '@/views/04-zixun'
import Register from '@/views/05-register'
import User from '@/views/06-user'
import Collect from '@/views/07-collect'
import Layout from '@/views/08-layout'
import Chuzu from '@/views/09-chuzu'
import Citylist from '@/views/10-citylist'
import Gochuzu from '@/views/11-gochuzu'
import Sousuo from '@/views/12-sousuo'
import Map from '@/views/13-map'
import { getToken } from '@/utils/storage'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    // redirect: '/index',
    children: [

      {
        path: '/find',
        component: Find
      },
      {
        path: '/index',
        component: Index
      },
      {
        path: '/zixun',
        component: Zixun
      },
      {
        path: '/user',
        // redirect: '/login',
        component: User
      }
    ]
  },
  {
    path: '/register',
    component: Register
  },

  {
    path: '/collect',
    component: Collect
  },
  {
    path: '/chuzu',
    component: Chuzu
  }, {
    path: '/citylist',
    component: Citylist
  },
  {
    path: '/gochuzu',
    component: Gochuzu
  },
  {
    path: '/sousuo',
    component: Sousuo
  },
  {
    path: '/map',
    component: Map
  }

]

const router = new VueRouter({
  routes
})
const whiteList = ['/user', '/find', '/zixun', '/index', '/login']
router.beforeEach((to, from, next) => {
  const token = getToken()
  // 有token直接放行
  if (token) { next() } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
